package com.hpq.user.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpq.common.domain.enums.YesOrNoEnum;
import com.hpq.user.domain.model.UserBackpack;
import com.hpq.user.mapper.UserBackpackMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserBackpackDAO extends ServiceImpl<UserBackpackMapper, UserBackpack> {
    /**
     *  获取用户背包中的徽章。
     * @param uid
     * @param itemIds
     * @return
     */
    public List<UserBackpack> getBadgesByItemId(Long uid, List<Long> itemIds) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .in(UserBackpack::getItemId, itemIds)
                .list();
    }

    /**
     * 获取用户背包中某物品的有效（或未使用）数量。
     * @param uid
     * @param itemId
     * @return
     */
    public Integer getCountByValidItemId(Long uid, Long itemId) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getId,itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    /**
     * 查询用户背包中有效物品
     * @param uid
     * @param itemId
     * @return
     */
    public UserBackpack getValidItemById(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getId)
                .last("limit 1")
                .one();
    }

    public boolean updateItem(UserBackpack modifyNameItem) {
        return lambdaUpdate()
                .eq(UserBackpack::getId, modifyNameItem.getId())
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus())
                .update();

    }

    public UserBackpack getByIdempotent(String idempotent) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent, idempotent)
                .one();
    }
}
